package com.gouqi.mall.service.impl;

import com.gouqi.mall.domain.Enterprise;
import com.gouqi.mall.domain.ProductBrand;
import com.gouqi.mall.domain.ResultJson;
import com.gouqi.mall.mapper.EnterpriseMapper;
import com.gouqi.mall.mapper.MessageMapper;
import com.gouqi.mall.service.EnterpriseService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Autowired
    private EnterpriseMapper mapper;
    private JSONObject json = new JSONObject();
    private ResultJson result = new ResultJson();


    @Override
    public String addOneCompany(Enterprise company) {
        Integer id = mapper.addOneCompany(company);
        if (id > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("新增一家企业信息成功！");
            result.setResultJson(id);
        } else {
            result.setStatus(404);
            result.setCode(204);
            result.setMessage("新增一家企业信息失败！");
            result.setResultJson("");
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String updateOneCompany(Enterprise company) {
        Integer id = mapper.updateOneCompany(company);
        if (id > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("更新一家企业信息成功！");
        } else {
            result.setStatus(404);
            result.setCode(204);
            result.setMessage("更新一家企业信息失败！");
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String deleteOneCompany(Integer id) {
        Integer res = mapper.deleteOneCompany(id);
        if (res > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("删除一家企业信息成功！");
            result.setResultJson(null);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("删除一家企业信息失败！");
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String queryOneCompany(String name) {
        List<Enterprise> enterprise= mapper.queryOneCompany(name);
        if (enterprise != null) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("查找一家企业信息成功！");
            result.setResultJson(enterprise);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("查找一家企业信息失败！");
            result.setResultJson(null);
        }
        json.put("result",result);
        return json.toString();
    }
    @Override
    public String queryAllCompanies(Integer index,Integer pageSize) {
        List<Enterprise> list= mapper.queryAllCompanies(index,pageSize);
        if (list != null) {
            //统计总品牌数，分页显示
            Integer total = mapper.queryCountCompanies();
            //余数
            Integer remainder = total%pageSize;
            //商
            Integer quotient = total/pageSize;
            Integer pageCount =  remainder == 0 ? quotient:quotient+1;
            System.out.println("remainder余数:"+remainder+"  quotient商:"+quotient);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("total",total);
            map.put("index",index);
            map.put("pageSize",pageSize);
            map.put("pageCount",pageCount);
            map.put("productBrandList",list);
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("查找所有企业信息成功！");
            result.setResultJson(map);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("查找所有企业信息失败！");
            result.setResultJson(null);
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String getAllCompanies(Integer index, Integer pageSize) {
        List<Enterprise> list= mapper.getCompanies(index,pageSize);
        if (list != null) {
            //统计总品牌数，分页显示
            Integer total = mapper.queryCountCompanies();
            //余数
            Integer remainder = total%pageSize;
            //商
            Integer quotient = total/pageSize;
            Integer pageCount =  remainder == 0 ? quotient:quotient+1;
            System.out.println("remainder余数:"+remainder+"  quotient商:"+quotient);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("total",total);
            map.put("index",index);
            map.put("pageSize",pageSize);
            map.put("pageCount",pageCount);
            map.put("productBrandList",list);
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("查找所有企业信息成功！");
            result.setResultJson(map);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("查找所有企业信息失败！");
            result.setResultJson(null);
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String getCompanyById(Integer id) {
        Enterprise enterprise= mapper.getCompanyById(id);
        if (enterprise != null) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("查找企业信息成功！");
            result.setResultJson(enterprise);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("查找企业信息失败！");
            result.setResultJson(null);
        }
        json.put("result",result);
        return json.toString();
    }
}
